<?php

namespace backend\modules\business\models;

use common\models\business\BusinessPigPrice;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\business\BusinessPigPriceDetail;

/**
 * BusinessPigPriceDetailSearch represents the model behind the search form about `common\models\business\BusinessPigPriceDetail`.
 */
class BusinessPigPriceDetailSearch extends BusinessPigPriceDetail
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'pigpriceid', 'pigid', 'status'], 'integer'],
            [['type', 'otherdesc', 'unit','pig.no','createtime','pigprice.number'], 'safe'],
            [['price', 'weight'], 'number'],
        ];
    }

    public function attributes()
    {
        return array_merge(parent::attributes(),['pig.no','pigprice.number']); // TODO: Change the autogenerated stub
    }


    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = BusinessPigPriceDetail::find()->alias('pd');

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);


        $dataProvider->sort->attributes['pig.no'] = [
            'asc' => ['pig.no' => SORT_ASC],
            'desc' => ['pig.no' => SORT_DESC],
        ];
        $dataProvider->sort->attributes['pigprice.number'] = [
            'asc' => ['pigprice.number' => SORT_ASC],
            'desc' => ['pigprice.number' => SORT_DESC],
        ];

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'pigpriceid' => $this->pigpriceid,
            'pigid' => $this->pigid,
            'pd.price' => $this->price,
            'pd.weight' => $this->weight,
            'pd.status' => $this->status,
        ]);

        if(!empty($this->createtime)){
            $date = explode(' - ',$this->createtime);
            $query->andFilterWhere(['between', 'pd.createtime', strtotime($date[0]), strtotime($date[1])]);
        }


        $query->andFilterWhere(['like', 'pd.type', $this->type])
            ->andFilterWhere(['like', 'pd.otherdesc', $this->otherdesc])
            ->andFilterWhere(['like', 'pig.no', $this->getAttribute('pig.no')])
            ->andFilterWhere(['like', 'pigprice.number', $this->getAttribute('pigprice.number')])
            ->andFilterWhere(['like', 'pd.unit', $this->unit]);

        return $dataProvider;
    }
}
